package com.wengu.linkedlist;


import org.junit.jupiter.api.Test;

import java.util.Stack;

/**
 * @Author 王海雷
 * @Date 2021/6/8 9:21
 * @Version 1.0
 */
public class SingleLinkedListDemo {

    public static void main(String[] args) {
        SingleLinkedList list = new SingleLinkedList();
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");

        // 按照编号有序插入元素
        list.insert(hero1);
        list.insert(hero4);
        list.insert(hero2);
        list.insert(hero3);
        System.out.println("==============倒序打印前的链表================");
        list.showList();
        // 倒序打印链表
        System.out.println("================倒序打印链表==================");
        list.inversionPrint();
        System.out.println("==============倒序打印后的链表================");
        list.showList();
    }

    @Test
    public void test1() {
        SingleLinkedList list1 = new SingleLinkedList();
        SingleLinkedList list2 = new SingleLinkedList();
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "公孙胜", "入云龙");
        HeroNode hero5 = new HeroNode(5, "关胜", "大刀");
        HeroNode hero6 = new HeroNode(6, "林冲", "豹子头");
        // 按照编号有序插入元素
        list1.insert(hero3);
        list1.insert(hero5);
        list1.insert(hero2);
        list2.insert(hero1);
        list2.insert(hero4);
        list2.insert(hero6);

        System.out.println("======单向链表1=====");
        list1.showList();
        System.out.println("======单向链表2=====");
        list2.showList();
        SingleLinkedList mergeList = SingleLinkedList.mergeList(list1, list2);
        System.out.println("======合并后的单向链表=====");
        mergeList.showList();
    }

}

class SingleLinkedList {

    public SingleLinkedList() {
        head = HeroNode.getInstance();
    }

    private HeroNode head;

    /**
     * 展示列表
     */
    public void showList() {
        if (head.next == null) {
            System.out.println("链表为空~~~");
            return;
        }
        HeroNode temp = head.next;
        while (true) {
            System.out.println(temp);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
    }

    /**
     * 在链表尾部添加元素
     *
     * @param node
     */
    public void add(HeroNode node) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                temp.next = node;
                break;
            }
            temp = temp.next;
        }
    }

    /**
     * 根据英雄编号有序插入相应位置
     *
     * @param node
     */
    public void insert(HeroNode node) {
        if (head.next == null) {
            head.next = node;
            return;
        }
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                flag = true;
                break;
            }
            if (node.no == temp.next.no) {
                break;
            }
            if (temp.next.no > node.no) {
                flag = true;
                break;
            }
            temp = temp.next;

        }
        if (flag) {
            node.next = temp.next;
            temp.next = node;
        } else {
            System.out.println("当前英雄编号已存在，不能插入");
        }
    }

    /**
     * 根据编号删除对应编号的英雄
     *
     * @param no 英雄编号
     */
    public void delete(int no) {
        if (head.next == null) {
            System.out.printf("链表不存在对应编号为 %d 的英雄", no);
            return;
        }
        boolean flag = false;
        HeroNode temp = this.head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                flag = true;
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            System.out.printf("删除编号为 %d 的英雄\n", no);
        } else {
            System.out.println("链表不存在对应编号的英雄");
        }

    }

    /**
     * 获取链表的元素的个数
     *
     * @return
     */
    public int getNumberOfList() {
        int number = 0;
        HeroNode temp = head.next;
        while (temp != null) {
            number++;
            temp = temp.next;
        }
        return number;
    }

    /**
     * 获取倒数第k个元素
     *
     * @param k
     */
    public void getHeroNodeByCountBackwards(int k) {
        if (head.next == null) {
            System.out.println("该列表不存在指定倒数序号的英雄");
            return;
        }
        int number = this.getNumberOfList();
        HeroNode temp = this.head.next;
        if (number < k || k <= 0) {
            System.out.println("该列表不存在指定倒数序号的英雄");
            return;
        }
        for (int j = 0; j < number - k; j++) {
            temp = temp.next;
        }
        System.out.println("该列表倒数第 " + k + " 的英雄为: " + temp);
    }

    /**
     * 反转单向链表
     */
    public void inversion() {
        if (head.next == null) {
            return;
        }
        // 思路：
        // 1.创建一个新的单向链表头inversionHead，
        // 2.将原链表中的元素以一个一个取出，并存入新链表的首个元素的位置，
        // 3.将head.next = inversionHead.next
        HeroNode inversionHead = new HeroNode(0, "", "");
        HeroNode temp = head.next;
        HeroNode first;
        while (temp != null) {
            first = inversionHead.next;
            inversionHead.next = temp;
            head.next = temp.next;
            temp.next = first;
            temp = head.next;
        }
        head.next = inversionHead.next;
    }

    /**
     * 倒序打印单向链表，并不改变链表结构
     * 使用Stack
     */
    public void inversionPrint() {
        if (head.next == null) {
            System.out.println("单向链表为空");
            return;
        }
        HeroNode temp = head.next;
        Stack<HeroNode> stack = new Stack<>();
        while (temp != null) {
            stack.push(temp);
            temp = temp.next;
        }
        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }

    /**
     * 合并两个有序单向链表，合并后的链表也是有序的
     * @param list1 单向列表1
     * @param list2 单向列表2
     * @return 合并后的有序单向链表
     */
    public static SingleLinkedList mergeList(SingleLinkedList list1, SingleLinkedList list2) {
        SingleLinkedList list = new SingleLinkedList();
        HeroNode temp1 = list1.head.next;
        HeroNode temp2 = list2.head.next;
        HeroNode temp;
        HeroNode newListHead = list.head;
        HeroNode last = newListHead;
        boolean flag = true;
        while (temp1 != null || temp2 != null) {
            if (temp1 != null && temp2 != null){
                if (temp1.no < temp2.no){
                    flag = true;
                    temp = temp1;
                }else{
                    flag = false;
                    temp = temp2;
                }
            }else if (temp1 != null){
                temp = temp1;
                flag = true;
            }else {
                temp = temp2;
                flag = false;
            }
            last.next = temp;
            if (flag){
                list1.head.next = temp1.next;
            }else{
                list2.head.next =  temp2.next;
            }
            temp.next = null;
            last = last.next;
            temp1 = list1.head.next;
            temp2 = list2.head.next;
        }
        return list;
    }
}

/**
 * 英雄节点类
 */
class HeroNode {
    int no;
    String name;
    String nickName;
    HeroNode next;

    public HeroNode() {
    }

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    public static HeroNode getInstance() {
        return new HeroNode(0, "", "");
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }

}
